package com.example.list_view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {

    private val image:Array<Int> = arrayOf(R.drawable.head01,R.drawable.head01,R.drawable.head01)
    private val name:Array<String> = arrayOf("张三","王五","六四")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val listView = findViewById<ListView>(R.id.list_view)
        val mutableList = mutableListOf<Map<String, Any>>()
        for (i in image.indices) {
            mutableList.add(mapOf(Pair("head_thumb", image[i]), Pair("name",name[i])))
        }
        listView.adapter = SimpleAdapter(this,mutableList, R.layout.main, arrayOf("head_thumb","name"),
            intArrayOf(R.id.head_thumb,R.id.name))

        listView.setOnItemClickListener{ parent: AdapterView<*>, view1: View, position: Int, id: Long ->
            val item = parent.getItemAtPosition(position) as Map<String, Any>
            Toast.makeText(this@MainActivity, item["name"].toString(),Toast.LENGTH_SHORT).show()
        }
    }
}
